<template>
<div class="box">
  <div class="login_container">
    <!-- 头像区域 -->
    <div class="logo">
      <img src="../assets/logo.png" alt="" />
    </div>

    <!-- 表单区域 -->
    <el-form
      label-width="0px"
      class="login_form"
      :model="loginForm"
      :rules="rulesForm"
      ref="FormRef"
    >
      <el-form-item prop="username" >
        <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" prefix-icon="iconfont icon-3702mima" v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-form-item class="btns">
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="info" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
        getdata:{},
      },

      rulesForm: {
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],

        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    login() {
      this.$refs.FormRef.validate((valid) => {
        if (!valid) {
           return
        } else {
          this.$http.post("login", this.loginForm).then((response) => {
            if (response.data.meta.status == 200){
               this.getdata=response.data
                this.$message.success("登录成功");
                  console.log(this.getdata);
                window.sessionStorage.setItem('token',this.getdata.data.token)
                this.$router.push('/home')  
          } else{
            this.$message.error('登录失败,用户名或密码错误')
          }
               });
        }
      });
    },

    reset() {
      this.$refs.FormRef.resetFields();
    },
  },

  mounted() {
    //   console.log(this)
  },
};
</script>

<style lang="less" scoped>
.box{
    height: 100%;
   background-color: #2b4b6b;
}
.login_container {
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .logo {
    width: 150px;
    height: 150px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 0 10px #eee;
    padding: 10px;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eeeeee;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0px 20px;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>